﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI.Control {
    /// <summary>
    /// 过滤表 Grid
    /// </summary>
    public class FTGrid : DataGridView {

        public void InitGrid() {
            AutoGenerateColumns = false;
            AllowUserToAddRows = false;
            AllowUserToDeleteRows = false;
            AllowUserToOrderColumns = false;
            AllowUserToResizeRows = false;
            AllowUserToResizeColumns = false;

            Rows.Clear();
            Columns.Clear();

            AddCol("序号");

            for (int i = 1; i < 15; i++)
                AddCol(i.ToString());

            AddCol("限制范围");

            AddCol("正确");

            AddCol("限制名称");

            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        public void SetErrorCellColor() {
            for (int i = 0; i < RowCount; i++) {
                var l = this[15, i];
                var c = this[16, i];
                if (l == null || l.Value == null) {
                    continue;
                }

                if (c == null || c.Value == null) {
                    continue;
                }

                var limit = l.Value.ToString().Split('-');
                int low, up, cellData;
                try {
                    low = Convert.ToInt32(limit[0].Trim());
                    up = Convert.ToInt32(limit[1].Trim());
                } catch {
                    continue;
                }

                try {
                    cellData = Convert.ToInt32(c.Value);
                } catch {
                    continue;
                }
                
                if (cellData < low || cellData > up)
                    c.Style.ForeColor = System.Drawing.Color.Red;
                else
                    c.Style.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void AddCol(string text) {
            var col = new DataGridViewTextBoxColumn();
            col.Visible = true;
            col.ReadOnly = true;
            col.HeaderText = text;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(col);
        }

        public void SetGridData(FTCellData data) {
            this.RowCount = data.RowCount;

            for (int row = 0; row < data.RowCount; row++) {
                for (int col = 0; col < data.ColCount; col++) {
                    this[col, row].Value = data[col, row];
                }
            }

            AutoResizeColumns();

            SetErrorCellColor();
        }

        public void HideLastTwoCols() {
            int col = ColumnCount - 1;
            this.Columns[col].Visible = false;

            col = ColumnCount - 2;
            this.Columns[col].Visible = false;
        }

        public void ShowLastTwoCols() {
            int col = ColumnCount - 1;
            this.Columns[col].Visible = true;

            col = ColumnCount - 2;
            this.Columns[col].Visible = true;
        }
    }
}
